package com.cysion.study.ui.fragment

import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.other.addTo
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.utils.ACache
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.PMoneyActivity
import com.cysion.study.ui.login.LoginActivity
import com.cysion.study.ui.login.UserSwitchActivity
import com.cysion.study.ui.personal.PersonalInfoActivity
import com.cysion.study.ui.personal.SettingActivity
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.fragment_me_p.*

/**
 * Created by chu on 2019-06-30.
 */
class PMeFragment : BaseFragment(), View.OnClickListener {

    val compositeDisposable: CompositeDisposable = CompositeDisposable()
    override fun getLayoutId(): Int = R.layout.fragment_me_p

    override fun initView() {
//        activity!!.darkTextTheme(activity!!.color(R.color.main_cl))
        cv_head.setOnClickListener(this)
        tv_userName.setOnClickListener(this)
        tv_teacher.setOnClickListener(this)
        tv_money.setOnClickListener(this)
        tv_attpe.setOnClickListener(this)
        tv_setting.setOnClickListener(this)



    }

    override fun onResume() {
        super.onResume()
        CLog.i("onResume")
        if (TextUtils.isEmpty(UserCache.token)) {
            Glide.with(context).load("").apply(
                RequestOptions().placeholder(R.mipmap.default_head)
            ).into(cv_head)
            tv_userName.text = UserCache.userInfo?.nickName ?: "未登录"
            tv_wait.text ="0"//待授课金额
            tv_waitMoney.text ="0"//可提现金额
            tv_allMoney.text ="0"//累计提现金额
            UIHelper.jumpForResult(activity,LoginActivity::class.java,100)
        } else {
            updateUserInfo()
        }
    }

    override fun onClick(v: View?) {
        if (TextUtils.isEmpty(UserCache.userId)) {
            context.startActivity_ex<LoginActivity>()
            return
        }
        when (v?.id) {
            R.id.cv_head -> {//个人信息
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_userName -> {
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_teacher -> {//切换
//                ShowToast("老师")
                UIHelper.jump(activity, UserSwitchActivity::class.java)
                context.finish()
                /*UserCache.clear()
                context.startActivity_ex<LoginActivity>()*/
            }
            R.id.tv_money -> {//钱包
//                context.startActivity_ex<MoneyActivity>()
                UIHelper.jump(activity, PMoneyActivity::class.java)
            }
            R.id.tv_attpe -> {//家教管理
//                context.startActivity_ex<PrivateManageActivity>()
                UIHelper.jump(activity, PManagerActivity::class.java)
            }
            R.id.tv_setting -> {//设置
                context.startActivity_ex<SettingActivity>()
            }

        }

    }


    override fun lazyLoad() {
        super.lazyLoad()
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("lazyLoad")
            context.startActivity_ex<LoginActivity>()
            return
        }

//        getUserInfos()

    }

    private fun getUserInfos() {
        ApiClient.api.getTeacherInfos(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (!TextUtils.isEmpty(it.id)) {
                        UserCache.save(it)
                        updateUserInfo()
                    }
                }
            }
    }

    private fun updateUserInfo() {
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("visibleAgain")
            context.startActivity_ex<LoginActivity>()
            return
        }
        if (TextUtils.isEmpty(UserCache.userInfo?.nickName ?: "")) {
            getUserInfos()
            return
        }
        Glide.with(context).load(UserCache.userInfo?.headImageUrl).apply(
            RequestOptions().placeholder(R.mipmap.default_head)
        ).into(cv_head)
        tv_userName.text = UserCache.userInfo?.nickName ?: "未登录"
        tv_wait.text =UserCache.userInfo?.feduCrsMoney//待授课金额
        tv_waitMoney.text =UserCache.userInfo?.feduMoney//可提现金额
        tv_allMoney.text =UserCache.userInfo?.feduOverWithMoney//累计提现金额

        var ttype= ACache.get(Box.context).getAsString("teacherType")
        println("####账号类型私教="+ttype)
        /*if (ttype == "0") {//普通教师首页fragment
            tv_teacher.text = "普通教师"
        } else {
            tv_teacher.text = "家教"
        }*/

    }

    override fun visibleAgain() {
        super.visibleAgain()
        CLog.i("visibleAgain中的token=" + UserCache.token)
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("visibleAgain")
            context.startActivity_ex<LoginActivity>()
            return
        }

    }

    override fun closeMvp() {
        compositeDisposable.dispose()
    }
}